import javax.print.attribute.standard.JobKOctets;


public class Test4 {


    public static Object locker1 =new Object();
    public static Object locker2 =new Object();
    public static Object locker3 =new Object();
    // 要注意内存可见性问题
    static volatile int count=1;
    static volatile int count1=1;


    public static void main(String[] args) {
        Thread a=new Thread(()->{
            synchronized (locker2){
                while(count1 != 3){
                    try {
                        locker2.wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
                System.out.println("a");
                count1++;
                locker2.notifyAll();
            }

        });Thread b=new Thread(()->{
            synchronized (locker2){
                while(count1 != 2){
                    try {
                        locker2.wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
                System.out.println("b");
                count1++;
                locker2.notifyAll();
            }

        });Thread c=new Thread(()->{
            synchronized (locker2){
                while(count1 != 1){
                    try {
                        locker2.wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
                System.out.println("c");
                count1++;
                locker2.notifyAll();
            }

        });
        a.start();
        b.start();
        c.start();
    }















    public static void main01(String[] args) throws InterruptedException {
        Thread t1=new Thread(new Runnable() {
            @Override
            public  void run() {
                    for(int i=0;i<10;i++){
                        synchronized (locker1){
                            while(count % 3 != 1){
                                try {
                                    locker1.wait();
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                            }System.out.print("A");
                            count++;
                            locker1.notifyAll();
                    }
                }
            }
        });Thread t2=new Thread(new Runnable() {
            @Override
            public  void run() {
                for(int i=0;i<10;i++){
                    synchronized (locker1){
                        while(count % 3 != 2){
                            try {
                                locker1.wait();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }System.out.print("B");
                        count++;
                        locker1.notifyAll();
                    }

                }

            }
        });Thread t3=new Thread(new Runnable() {
            @Override
            public  void run() {
                for(int i=0;i<10;i++){
                    synchronized (locker1){
                        while(count % 3 != 0){
                            try {
                                locker1.wait();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                        System.out.println("C");
                        count++;
                        locker1.notifyAll();
                    }
                }
            }
        });
        t1.start();
        t2.start();
        t3.start();

    }
}








